package enhance.day02;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class PlayDemo1 extends JFrame {

    Image ab;
    Image cy;
    int backWidth = 700;
    int backHeight = 700;

    int cyx = 250;
    int cyy = 350;
    int c = 1;
    int d = 1;
    int flag = 1;

    /**
     * 图形界面窗口渲染出图像
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        //super.paint(g);
        // 绘制angelababy
        g.drawImage(ab,0,0,backWidth,backHeight,this);

        // 绘制苍蝇  cyx参数是苍蝇的x坐标，cyy参数是苍蝇的y坐标
        g.drawImage(cy,cyx,cyy,60,60,this);

        /*if(cyx<=450) {
            cyx = cyx + 1;
            cyy = (int) (Math.pow(10000 - Math.pow(cyx - 350, 2), 0.5) + 350);
        }else{
            cyx = cyx -1;
            cyy = (int) (-Math.pow(10000 - Math.pow(cyx - 350, 2), 0.5) + 350);
        }*/

        if(cyx>450 || cyx < 250){
            flag = -flag;
        }
        cyx = cyx + 1*flag;
        cyy = (int) (flag*Math.pow(10000 - Math.pow(cyx - 350, 2), 0.5) + 350);

    }


    @Override
    protected void frameInit() {
        super.frameInit();
        ab = Toolkit.getDefaultToolkit().createImage("data/image/ab.png");
        cy = Toolkit.getDefaultToolkit().createImage("data/image/tmcy.png");

        try {
            BufferedImage ab = ImageIO.read(new File("data/image/ab.png"));
            backHeight = ab.getHeight();
            backWidth = ab.getWidth();

        } catch (IOException e) {
            e.printStackTrace();
        }

        setSize(backWidth,backHeight);
        setLocation(100,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    /**
     * 程序的入口
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // 创建了这个游戏类的对象
        // 本质创建一个图形化窗口对象
        PlayDemo1 demo1 = new PlayDemo1();
        demo1.setTitle("多易教育-ab与蝇蝇的邂逅");

        // 对图形界面进行初始化
        demo1.frameInit();

        // 无限循环
        while (true){
            // 调用窗口对象的重画方法
            demo1.repaint(1000);

            // 暂停100ms
            Thread.sleep(10);
        }
    }
}
